<?php
namespace Home\Controller;
use Admin\Model\ConfigModel;
use Message\Logic\MessageLogic;
use Think\Verify;
use Think\Controller;

class ActionController extends Controller {

    /**
     * 默认返回信息
     */
    protected $ret = array('errNum'=>0, 'errMsg'=>'success', 'retData'=>array());

    /**
     * 构造函数
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 返回正确的格式的json数据
     */
    private function setjsonReturn($value, $key=null)
    {
        if (isset($key)) {
            $this->ret['retData'][$key]=$value;
        } else {
            $this->ret['retData']=$value;
        }
        $this->ajaxReturn ($this->ret,'JSON');
    }

    /**
     * 返回错误格式的json数据
     */
    private function errorjsonReturn($value)
    {
        $this->ret['errNum']=-1;
        $this->ret['errMsg']=$value;
        $this->ajaxReturn ($this->ret,'JSON');
    }

    /**
     * 生成图片验证码
     */
    public function make_code()
    {
        $Verify = new Verify(array('fontSize' => 20, 'length' =>4, 'useCurve' => false, 'useNoise' => true, 'reset' => true));
        $Verify->entry("valid_code");
    }

    /**
     * 发送短信验证码
     */
    public function make_mobile_code()
    {
        $post=I('post.');
        if(!$post['mobile']){$this->errorjsonReturn('请填写手机号');}
        if(!is_phone($post['mobile'])){$this->errorjsonReturn('手机号码格式错误');}
        $return=(new MessageLogic())->addMessage('customer_apply',$post['mobile']);
        $return===true?$this->setjsonReturn('发送成功'):$this->errorjsonReturn('发送失败');
    }

    /**
     * 客户申请提交
     */
    public function apply()
    {
        $post=I('post.');
        foreach($post as $k=>$value){$post[$k]=trim($value);}
        if(!$post['name']){$this->errorjsonReturn('请填写姓名');}
        if(!$post['money']){$this->errorjsonReturn('请填写金额');}
        if(!is_numeric($post['money'])){$this->errorjsonReturn('金额格式错误');}
        if(!$post['city']){$this->errorjsonReturn('请选择地区');}
        if(!$post['mobile']){$this->errorjsonReturn('请填写手机号');}
        if(!is_phone($post['mobile'])){$this->errorjsonReturn('手机号码格式错误');}
        if(!(new Verify())->check($post['code'], "valid_code")){$this->errorjsonReturn('验证码错误');}
        $key=(new ConfigModel())->getConfigByNameModel('pc_guangwang_push_key');
        $return=post_url('http://'.$_SERVER['SERVER_NAME'].U('api/index/add'),array(
            'name'=>$post['name'],
            'mobile'=>$post['mobile'],
            'city'=>$post['city'],
            'money'=>$post['money']*10000,
            'key'=>$key
        ));
        if(!$return){$this->setjsonReturn('申请失败');}
        $return=json_decode($return,true);
        if($return['errNum']!==0){$this->setjsonReturn('申请失败');}
        //下面发送申请成功的短信
        (new MessageLogic())->addMessage('apply_success',$post['mobile']);
        $this->setjsonReturn('申请成功');
    }

    /**
     * 手机端的客户申请
     */
    public function apply_mobile()
    {
        $post=I('post.');
        foreach($post as $k=>$value){$post[$k]=trim($value);}
        if(!$post['name']){$this->errorjsonReturn('请填写姓名');}
        if(!$post['money']){$this->errorjsonReturn('请填写金额');}
        if(!is_numeric($post['money'])){$this->errorjsonReturn('金额格式错误');}
        if(!$post['city']){$this->errorjsonReturn('请选择地区');}
        if(!$post['mobile']){$this->errorjsonReturn('请填写手机号');}
        if(!is_phone($post['mobile'])){$this->errorjsonReturn('手机号码格式错误');}
        if(!(new Verify())->check($post['code'], "valid_code")){$this->errorjsonReturn('验证码错误');}
        $key=(new ConfigModel())->getConfigByNameModel('mobile_guangwang_push_key');
        $return=post_url('http://'.$_SERVER['SERVER_NAME'].U('api/index/add'),array(
            'name'=>$post['name'],
            'mobile'=>$post['mobile'],
            'city'=>$post['city'],
            'money'=>$post['money']*10000,
            'key'=>$key,
        ));
        if(!$return){$this->setjsonReturn('申请失败');}
        $return=json_decode($return,true);
        if($return['errNum']!==0){$this->setjsonReturn('申请失败');}
        //下面发送申请成功的短信
        (new MessageLogic())->addMessage('apply_success',$post['mobile']);
        $this->setjsonReturn('申请成功');
    }

    /**
     * 提交建议
     */
    public function suggest()
    {
        $post=I('post.');
        foreach($post as $k=>$value){$post[$k]=trim($value);}
        if(!$post['type']){$this->errorjsonReturn('请选择类型');}
        if(!$post['user_name']){$this->errorjsonReturn('请填写姓名');}
        if(!$post['mobile']){$this->errorjsonReturn('请填写手机号');}
        if(!is_phone($post['mobile'])){$this->errorjsonReturn('手机号码格式错误');}
        if(!$post['city']){$this->errorjsonReturn('请选择城市');}
        if(!$post['content']){$this->errorjsonReturn('请完善内容');}
        if(!(new Verify())->check($post['code'], "valid_code")){$this->errorjsonReturn('验证码错误');}
        $post['platform']='pc官网';
        $post['create_time']=time();
        M('suggest')->add($post)?$this->setjsonReturn('提交成功'):$this->setjsonReturn('提交失败');
    }
}